package com.xul.yygh.order.controller.api;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xul.yygh.common.result.Result;
import com.xul.yygh.common.utils.AuthContextHolder;
import com.xul.yygh.order.service.OrderInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import model.com.xul.yygh.enums.OrderStatusEnum;
import model.com.xul.yygh.model.order.OrderInfo;
import model.com.xul.yygh.vo.order.OrderCountQueryVo;
import model.com.xul.yygh.vo.order.OrderQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author xul
 * @create 2021-07-19 17:26
 */

@Api(tags = "订单接口")
@RestController
@RequestMapping("api/order/orderInfo")
public class OrderApiController {

    @Autowired
    private OrderInfoService orderInfoService;

    /**
     * 生成挂号订单
     *
     * @param scheduleId
     * @param patientId
     * @return
     */
    @ApiOperation("生成挂号订单")
    @PostMapping("auth/submitOrder/{scheduleId}/{patientId}")
    public Result saveOrders(@PathVariable String scheduleId,
                             @PathVariable Long patientId) {
        Long orderId = orderInfoService.saveOrder(scheduleId, patientId);
        return Result.ok(orderId);
    }

    /**
     * 根据订单id查询订单信息
     *
     * @param orderId
     * @return
     */
    @ApiOperation("根据订单id查询订单信息")
    @GetMapping("auth/getOrders/{orderId}")
    public Result getOrders(@PathVariable String orderId) {
        OrderInfo orderInfo = orderInfoService.getOrders(orderId);
        return Result.ok(orderInfo);
    }

    /**
     * 订单列表 条件查询带带分页
     *
     * @param page
     * @param limit
     * @param orderQueryVo
     * @param request
     * @return
     */
    @ApiOperation("订单列表 条件查询带带分页")
    @GetMapping("auth/{page}/{limit}")
    public Result list(@PathVariable Long page,
                       @PathVariable Long limit,
                       OrderQueryVo orderQueryVo,
                       HttpServletRequest request) {
        // 设置当前用户的id
        orderQueryVo.setUserId(AuthContextHolder.getUserId(request));
        Page<OrderInfo> pageParam = new Page<>(page, limit);
        IPage<OrderInfo> pageModel = orderInfoService.selectPage(pageParam, orderQueryVo);
        return Result.ok(pageModel);
    }

    /**
     * 获取订单状态
     *
     * @return
     */
    @ApiOperation("获取订单状态")
    @GetMapping("auth/getStatusList")
    public Result getStatusList() {
        return Result.ok(OrderStatusEnum.getStatusList());
    }

    /**
     * 取消预约
     * @param orderId
     * @return
     */
    @ApiOperation("取消预约")
    @GetMapping("auth/cancelOrder/{orderId}")
    public Result cancelOrder(@PathVariable Long orderId) {
        Boolean isOrder = orderInfoService.cancelOrder(orderId);
        return Result.ok(isOrder);
    }

    /**
     * 获取订单统计数据
     * @param orderCountQueryVo
     * @return
     */
    @ApiOperation("获取订单统计数据")
    @PostMapping("inner/getCountMap")
    public Map<String,Object> getCountMap(@RequestBody OrderCountQueryVo orderCountQueryVo){
        return orderInfoService.getCountMap(orderCountQueryVo);
    }



}
